// --------------------------------------------------------------------------------------------------------------------
// <copyright file="CameraPoseEstimatorAgent.cs" company="Microsoft Corporation">
// The MIT License (MIT)
// 
// Copyright (c) 2014, Microsoft Corporation
// 
// Permission is hereby granted, free of charge, to any person obtaining a copy
//  of this software and associated documentation files (the "Software"), to deal
//  in the Software without restriction, including without limitation the rights
//  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//  copies of the Software, and to permit persons to whom the Software is
//  furnished to do so, subject to the following conditions:
// 
// The above copyright notice and this permission notice shall be included in
//  all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//  THE SOFTWARE.
// </copyright>
// --------------------------------------------------------------------------------------------------------------------
namespace Microsoft.Robotics.Vision.Runtime.Cameras
{
    using System;
    using System.Collections.Generic;
    using System.Drawing;
    using System.Linq;
    using System.Runtime.Serialization;
    using System.Text;
    using System.Threading.Tasks;

    using Microsoft.Robotics.Numerics;
    using Microsoft.Robotics.Runtime;
    using Microsoft.Robotics.Vision.Cameras;
    using Microsoft.Robotics.Vision.Cameras.Kinect;
    using Microsoft.Robotics.Vision.GroundTruthExtraction;
    using Microsoft.Robotics.Vision.ImageProcessing2D;

    /// <summary>
    /// Agent that estimates the pose of a camera when viewing a checkerboard
    /// </summary>
    /// <typeparam name="TColorParameters">The color camera parameters</typeparam>
    [DataContract]
    public sealed class CameraPoseEstimatorAgent<TColorParameters> : ConsumerProducerAgent<ColorCameraAgentMessage<TColorParameters>, SimpleAgentMessage<Pose>>,
        IDisposable
        where TColorParameters : ColorParameters
    {
        /// <summary>
        /// Estimates the 3D pose of the camera
        /// </summary>
        private CameraPoseEstimation poseEstimation;

        /// <summary>
        /// Initializes a new instance of the CameraPoseEstimatorAgent class
        /// </summary>
        /// <param name="name">Name of the agent</param>
        /// <param name="colorProducer">producer of color images</param>
        public CameraPoseEstimatorAgent(
            string name,
            IProducer<ColorCameraAgentMessage<TColorParameters>> colorProducer)
            : base(name, colorProducer.Name)
        {
            this.poseEstimation = null;
        }

        /// <summary>
        /// Called on deactivation
        /// </summary>
        public override void OnDeactivated()
        {
            base.OnDeactivated();
            this.Dispose();
        }

        /// <summary>
        /// Disposes the pose estimation class
        /// </summary>
        public void Dispose()
        {
            if (this.poseEstimation != null)
            {
                this.poseEstimation.Dispose();
            }
        }

        /// <summary>
        /// Called on receive of a color camera agent message
        /// </summary>
        /// <param name="message">The received message</param>
        public override void Receive(ColorCameraAgentMessage<TColorParameters> message)
        {
            if (this.poseEstimation == null)
            {
                this.poseEstimation = new CameraPoseEstimation(message.Parameters.Intrinsics, message.Parameters.DistortionParameters);
            }

            Tuple<Pose, bool> result = this.poseEstimation.EstimatePose(message.ImageFrame, message.Parameters.Intrinsics, message.Parameters.DistortionParameters);

            if (result.Item2 == true)
            {
                Publisher.Post(new SimpleAgentMessage<Pose>(result.Item1, message.OriginatingTime));
            }
        }
    }
}
